package com.umeox.quranplayer.ui

import android.app.Activity
import android.content.Intent
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.quranplayer.ui.adapter.MusicListAdapter
import com.umeox.quranplayer.R
import com.umeox.quranplayer.repository.INTENT_KEY_AUDIO_NAME
import com.umeox.quranplayer.repository.INTENT_KEY_LIST_TYPE
import com.umeox.quranplayer.repository.LIST_TYPE_QURAN
import com.umeox.quranplayer.repository.QuranAudioManager
import com.yc.music.inter.OnPlayerEventListener
import com.yc.music.model.AudioBean
import com.yc.music.tool.BaseAppHelper

class QuranListActivity : ThemeAppCompatActivity(), MusicListAdapter.ItemClickListener,
    OnPlayerEventListener {

    private var rvMusics: RecyclerView? = null

    private lateinit var musicsAdapter: MusicListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quran_list)
        initView()
        initPlayServiceListener()
        window.decorView.post {
            val helper = BaseAppHelper.get()
            val playingMusic = helper.playService.playingMusic
            musicsAdapter.setCurAudioTitle(playingMusic?.title?:"")
            musicsAdapter.setDataList(QuranAudioManager.quranList)
        }
    }

    private fun initView() {
        rvMusics = findViewById(R.id.rv_musics)

        musicsAdapter = MusicListAdapter()
        rvMusics?.adapter = musicsAdapter
        rvMusics?.setHasFixedSize(true)
        rvMusics?.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        rvMusics?.addItemDecoration(object :RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                if(parent.getChildLayoutPosition(view) == QuranAudioManager.quranList.size - 1) {
                    outRect.bottom = 48
                }
            }
        })
        musicsAdapter.itemClickListener = this
    }

    private fun initPlayServiceListener() {
        if (BaseAppHelper.get().musicService == null) {
            return
        }
        BaseAppHelper.get().musicService.addOnPlayEventListener(this)
    }


    override fun onItemClicked(position: AudioBean) {
        setResult(Activity.RESULT_OK, Intent().apply {
            putExtra(INTENT_KEY_LIST_TYPE,LIST_TYPE_QURAN)
            putExtra(INTENT_KEY_AUDIO_NAME,position.title)
        })
        finish()
    }

    override fun onChange(music: AudioBean?) {
        musicsAdapter.setCurAudioTitle(music?.title?:"")
        musicsAdapter.setDataList(QuranAudioManager.quranList)
    }

    override fun onPlayerStart() {
    }

    override fun onPlayerPause() {
    }

    override fun onCompletion() {
    }

    override fun onUpdateProgress(progress: Int) {
    }

    override fun onBufferingUpdate(percent: Int) {
    }

    override fun onTimer(remain: Long) {
    }

    override fun onDuration(totalDuration: Int) {

    }

    override fun onDestroy() {
        super.onDestroy()
        BaseAppHelper.get().musicService?.removeOnPlayEventListener(this)
    }
}